#include <vector>
#include <string>
#include <unordered_map>
using namespace std;

class Solution {
public:
    bool issubstr(vector<string>& words, string& s)
    {
        unordered_map<string, int> map;
        for (auto& e : words)
        {
            map[e]++;
        }
        int onewords = words[0].size();
        int i = 0;
        while (i < s.size())
        {
            if (i + onewords > s.size()) {
                return false;
            }
            string str(s.begin() + i, s.begin() + onewords + i);
            if (map[str] != 0)
            {
                map[str]--;
            }
            else
            {
                return false;
            }
            i += onewords;
        }
        for (const auto& p : map) {
            if (p.second != 0) {
                return false;
            }
        }
        return true;
    }

    vector<int> findSubstring(string s, vector<string>& words) {
        vector<int> v;
        if (s.size() == 0 || words.size() == 0)
            return v;
        int onewords = words[0].size();
        int len = onewords * words.size();
        int i = 0;
        while (i <= (s.size() - len))
        {
            string str(s.begin() + i, s.begin() + i + len);
            if (issubstr(words, str))
                v.push_back(i);
            i += 1;
        }
        return v;
    }
};

int main()
{
    Solution s;
    vector<string> words{ "fooo","barr","wing","ding","wing" };
    string s1("lingmindraboofooowingdingbarrwingmonkeypoundcake");
    s.findSubstring(s1, words);
    return 0;
}